package com.zhiyizhen.handheld.component.message.util;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.view.ViewCompat;

import com.zhiyizhen.handheld.R;

import java.util.ArrayList;
import java.util.List;

public class PageNavigationHelper {
    private Context mContext;
    private LinearLayout mPageContainer;
    private View mPrevButton;
    private View mNextButton;
    private OnPageSelectedListener mListener;
    private int mTotalPages;
    private int mCurrentPage = 1;
    private TextView[] mPageButtons;

    // 回调接口，页码选中时触发
    public interface OnPageSelectedListener {
        void onPageSelected(int page);
    }

    public PageNavigationHelper(Context context, LinearLayout pageContainer,
                                View prevButton, View nextButton,
                                OnPageSelectedListener listener) {
        this.mContext = context;
        this.mPageContainer = pageContainer;
        this.mPrevButton = prevButton;
        this.mNextButton = nextButton;
        this.mListener = listener;

        // 上一页按钮点击事件
        mPrevButton.setOnClickListener(v -> goToPrevPage());
        // 下一页按钮点击事件
        mNextButton.setOnClickListener(v -> goToNextPage());
    }

    // 初始化页码
    public void init(int totalPages, int currentPage) {
        this.mTotalPages = totalPages;
        this.mCurrentPage = currentPage;
        mPageContainer.removeAllViews();

        // 创建页码按钮
        createPageButtons();
        // 更新导航按钮状态
        updateNavButtons();
        // 高亮当前页
        highlightCurrentPage();
    }

    // 创建所有页码按钮
    private void createPageButtons() {
        mPageButtons = new TextView[mTotalPages];

        for (int i = 0; i < mTotalPages; i++) {
            final int page = i + 1; // 页码从1开始
            TextView button = new TextView(mContext);

            // 设置按钮属性
            button.setText(String.valueOf(page));
            button.setTextSize(15);
            button.setGravity(Gravity.CENTER);
            button.setPadding(dpToPx(12), dpToPx(5), dpToPx(12), dpToPx(5));

            // 设置布局参数
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            params.setMargins(dpToPx(4), 0, dpToPx(4), 0);
            button.setLayoutParams(params);

            // 页码点击事件
            button.setOnClickListener(v -> {
                mCurrentPage = page;
                mListener.onPageSelected(page);
                highlightCurrentPage();
                scrollToCurrentPage();
                updateNavButtons();
            });

            mPageContainer.addView(button);
            mPageButtons[i] = button;
        }
    }

    // 高亮当前页
    private void highlightCurrentPage() {
        for (int i = 0; i < mPageButtons.length; i++) {
            TextView button = mPageButtons[i];
            int page = i + 1;

            if (page == mCurrentPage) {
                // 选中状态
                button.setTextColor(Color.WHITE);
                button.setBackgroundResource(R.drawable.shape_primary);
            } else {
                // 未选中状态
                button.setTextColor(Color.BLACK);
                button.setBackgroundResource(R.drawable.shape_gray_border);
            }
        }
    }

    // 滚动到当前页，确保可见
    private void scrollToCurrentPage() {
        if (mCurrentPage <= 0 || mCurrentPage > mPageButtons.length) {
            return;
        }

        TextView currentButton = mPageButtons[mCurrentPage - 1];
        // 延迟滚动，确保视图已绘制
        currentButton.post(() -> {
            HorizontalScrollView scrollView = (HorizontalScrollView) mPageContainer.getParent();
            scrollView.scrollTo(currentButton.getLeft() - dpToPx(40), 0);
        });
    }

    // 更新上一页/下一页按钮状态
    private void updateNavButtons() {
        mPrevButton.setVisibility(mCurrentPage > 1 ? View.VISIBLE : View.GONE);
        mNextButton.setVisibility(mCurrentPage < mTotalPages ? View.VISIBLE : View.GONE);
    }

    // 上一页
    public void goToPrevPage() {
        if (mCurrentPage > 1) {
            mCurrentPage--;
            mListener.onPageSelected(mCurrentPage);
            highlightCurrentPage();
            scrollToCurrentPage();
            updateNavButtons();
        }
    }

    // 下一页
    public void goToNextPage() {
        if (mCurrentPage < mTotalPages) {
            mCurrentPage++;
            mListener.onPageSelected(mCurrentPage);
            highlightCurrentPage();
            scrollToCurrentPage();
            updateNavButtons();
        }
    }

    // dp转px工具方法
    private int dpToPx(int dp) {
        float density = mContext.getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5f);
    }
}
    